﻿using System;
using System.Collections.Generic;

// 账户类
public class Account
{
    public string AccountNumber { get; private set; }
    public string Owner { get; set; }
    protected decimal Balance;

    public Account(string accountNumber, string owner, decimal initialBalance)
    {
        AccountNumber = accountNumber;
        Owner = owner;
        Balance = initialBalance;
    }

    public virtual void Deposit(decimal amount)
    {
        Balance += amount;
        Console.WriteLine($"{Owner} 存款 {amount}，余额: {Balance}");
    }

    public virtual bool Withdraw(decimal amount)
    {
        if (amount <= Balance)
        {
            Balance -= amount;
            Console.WriteLine($"{Owner} 取款 {amount}，余额: {Balance}");
            return true;
        }
        Console.WriteLine("余额不足！");
        return false;
    }

    public void ShowBalance()
    {
        Console.WriteLine($"账户 {AccountNumber} 余额: {Balance}");
    }
}



// 银行类
public class Bank
{
    //记录账号
    private Dictionary<string, Account> accounts = new Dictionary<string, Account>();

    public void AddAccount(Account account)
    {
        accounts[account.AccountNumber] = account;
    }

    // 利用索引器按账户号访问账户
    public Account this[string accountNumber]
    {
        get => accounts.ContainsKey(accountNumber) ? accounts[accountNumber] : null;
    }
}


// ATM
public class ATM
{
    public void Withdraw(Account account, decimal amount)
    {
        if (account != null)
            account.Withdraw(amount);
    }
}

// 信用账户类
public class CreditAccount : Account
{
    public decimal CreditLimit { get; private set; }

    //构造
    public CreditAccount(string accountNumber, string owner, decimal initialBalance, decimal creditLimit)
        : base(accountNumber, owner, initialBalance)
    {
        CreditLimit = creditLimit;
    }

    public override bool Withdraw(decimal amount)
    {
        //判断是否超出额度
        if (amount <= Balance + CreditLimit)
        {
            Balance -= amount;
            Console.WriteLine($"{Owner} 取款 {amount} (信用额度: {CreditLimit})，余额: {Balance}");
            return true;
        }
        Console.WriteLine("超出信用额度！");
        return false;
    }
}



